package com.google.code.synchronized_player;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.util.Log;
	
public class Server implements Runnable{
	
public void phoneServer() {

Log.i("Server", "Starting Server...");

try {
    ServerSocket ss = new ServerSocket(27960);
    Log.i("Server", "Server status: " + ss.isBound());
    byte len[] = new byte [1];

    while(true){
        Socket s = ss.accept();
        Log.i("Server", "Server is accepting connections: " + s.isConnected());
 
       
        BufferedReader input = new BufferedReader( new InputStreamReader(s.getInputStream()));
        char[] strbuffer = new char[ 1 ];
        input.read(strbuffer,0,1);
        byte length = (byte)(strbuffer[0]);
        char[] str = new char[length];
        input.read(str, 0, length);
        

        Log.i("Server",new String(str));
        
       // BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
        //PrintWriter output = new PrintWriter(s.getOutputStream(),true);
//        Log.i("Server", "Initialized input and output");
//        String st = input.readLine();
//        byte [] byteArr1 = st.getBytes("US-ASCII");
//        buf[0] = byteArr1[0];
//        byte buffer [] = new byte [buf[0]];
        
//        Log.i("Server", "From client: "+ st);
        
        
       // output.println("Good bye and thanks for all the fish");
        s.close();
        
//        Log.d("Server", "First Symbol: " + buffer.length);
    }
} catch (UnknownHostException e) { 
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
}

@Override
public void run() {
	// TODO Auto-generated method stub
	phoneServer();
	
}
}